package com.zzyl.service.impl;


import cn.hutool.core.date.LocalDateTimeUtil;
import com.aliyun.iot20180120.models.QueryDevicePropertyStatusRequest;
import com.aliyun.iot20180120.models.QueryDevicePropertyStatusResponseBody;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.entity.DeviceData;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.service.DeviceService;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;


import com.github.pagehelper.Page;

import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description DeviceDataServiceImpl
 * @Author huangdongye
 * @Date 2025-05-22  17:02
 */
@Service
public class DeviceDataServiceImpl implements DeviceDataService {
    @Autowired
    private DeviceDataMapper deviceDataMapper;

    /**
     * 分页查询
     *
     * @param deviceDataPageReqDto
     * @return
     */
    @Override
    public PageResponse<DeviceDataVo> page(DeviceDataPageReqDto deviceDataPageReqDto) {
        LocalDateTime localDateTimeStart = LocalDateTimeUtil.of(deviceDataPageReqDto.getStartTime());
        LocalDateTime localDateTimeEnd = LocalDateTimeUtil.of(deviceDataPageReqDto.getEndTime());
        deviceDataPageReqDto.setStartDate(localDateTimeStart);
        deviceDataPageReqDto.setEndDate(localDateTimeEnd);
        PageHelper.startPage(deviceDataPageReqDto.getPageNum(), deviceDataPageReqDto.getPageSize());

        Page<DeviceData> pageResponse = deviceDataMapper.Pageselect(deviceDataPageReqDto);
        return PageResponse.of(pageResponse, DeviceDataVo.class);
    }

    // 引入设备服务
    @Autowired
    private DeviceServiceImpl deviceServiceImpl;

    /**
     * 查询设备属性状态
     *
     * @param Request
     * @return
     */

    @Override
    public QueryDevicePropertyStatusResponseBody.QueryDevicePropertyStatusResponseBodyData QueryDevicePropertyStatus(QueryDevicePropertyStatusRequest request) {

        return deviceServiceImpl.QueryDevicePropertyStatus(request);
    }

    /**
     * 按日查询设备数据
     *
     * @param iotId
     * @param functionId
     * @param startTime
     * @param endTime
     * @return List<DeviceDataGraphVo>
     */

    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByDay(String iotId, String functionId, Long startTime, Long endTime) {
        // 查询数据库
        List<DeviceDataGraphVo> resultList = deviceDataMapper.queryDeviceDataListByDay(iotId, functionId, LocalDateTimeUtil.of(startTime), LocalDateTimeUtil.of(endTime));
        // 返回24小时,时间段的数据
        List<DeviceDataGraphVo> deviceDataGraphVos = DeviceDataGraphVo.dayInstance(LocalDateTimeUtil.of(startTime));
        // 循环判断时间相等插入
        for (DeviceDataGraphVo deviceDataGraphVo : deviceDataGraphVos) {
            for (DeviceDataGraphVo result : resultList) {
                if (deviceDataGraphVo.getDateTime().equals(result.getDateTime())) {
                    deviceDataGraphVo.setDataValue(result.getDataValue());
                }
            }
        }
        return deviceDataGraphVos;
    }

    /**
     * 按周查询设备数据
     *
     * @param iotId
     * @param functionId
     * @param startTime
     * @param endTime
     * @return List<DeviceDataGraphVo>
     */
    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByWeek(String iotId, String functionId, Long startTime, Long endTime) {
        // 查询数据库
        List<DeviceDataGraphVo> resultList = deviceDataMapper.queryDeviceDataListByWeek(iotId, functionId, LocalDateTimeUtil.of(startTime), LocalDateTimeUtil.of(endTime));
        // 返回7天,时间段的数据
        List<DeviceDataGraphVo> deviceDataGraphVos = DeviceDataGraphVo.weekInstance(LocalDateTimeUtil.of(startTime));
        // 循环判断时间相等插入
        for (DeviceDataGraphVo deviceDataGraphVo : deviceDataGraphVos) {
            for (DeviceDataGraphVo result : resultList) {
                if (deviceDataGraphVo.getDateTime().equals(result.getDateTime())) {
                    deviceDataGraphVo.setDataValue(result.getDataValue());
                }
            }
        }
        return deviceDataGraphVos;
    }
}
